package com.cenwangkeji.resident.ui.activity;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

import com.cenwangkeji.resident.R;
import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.bean.User;
import com.cenwangkeji.resident.ui.fragment.FileFragment;
import com.cenwangkeji.resident.ui.fragment.HomeFragment;
import com.cenwangkeji.resident.ui.fragment.MyFragment;
import com.cenwangkeji.resident.ui.fragment.QuestionFragment;
import com.cenwangkeji.resident.ui.fragment.ServicePackageFragment;
import com.lws.basic.base.BaseActivity;
import com.lws.basic.utils.activity.AppManager;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.SharePreferenceUtils;

import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

public class MainActivity extends BaseActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

    @BindView(R.id.bnv)
    BottomNavigationView mBnv;
    @BindView(R.id.iv)
    ImageView mIv;//中间按钮
    HomeFragment mHomeFragment;
    FileFragment mFileFragment;
    QuestionFragment mQuestionFragment;
    MyFragment mMyFragment;
    ServicePackageFragment mServicePackageFragment;

    @Override
    public int setLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        mBnv.setOnNavigationItemSelectedListener(this);
        selectFragment(0);
       // MyLogger.i(SharePreferenceUtils.getString(this, Constant.phone, ""));
        JPushInterface.setAlias(this, 1, SharePreferenceUtils.getString(this, Constant.phone, ""));//设置别名

    }

    @Override
    public void onBackPressed() {
        showBackDialog();

    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.navigation_home:
                selectFragment(0);//首页
                break;
            case R.id.navigation_file://服务
                selectFragment(1);
                break;
            case R.id.navigation_://档案
                selectFragment(2);
                break;
            case R.id.navigation_question://寻问药
                selectFragment(3);
                break;
            case R.id.navigation_my://
                selectFragment(4);
                break;
        }
        return true;
    }


    //显示选中fragment
    private void selectFragment(int i) {
        FragmentManager mFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        hideFragment(fragmentTransaction);
        switch (i) {
            case 0:
                if (mHomeFragment == null) {
                    mHomeFragment = new HomeFragment();
                    fragmentTransaction.add(R.id.fl, mHomeFragment);
                } else {
                    fragmentTransaction.show(mHomeFragment);
                }
                break;
            case 1:
                if (mServicePackageFragment == null) {
                    mServicePackageFragment = new ServicePackageFragment();
                    fragmentTransaction.add(R.id.fl, mServicePackageFragment);
                } else {
                    fragmentTransaction.show(mServicePackageFragment);
                }
                break;

            case 2:
                if (mFileFragment == null) {
                    mFileFragment = new FileFragment();
                    fragmentTransaction.add(R.id.fl, mFileFragment);
                } else {
                    fragmentTransaction.show(mFileFragment);
                }
                mIv.setImageResource(R.mipmap.ic_file_2);
                break;
            case 3:
                if (mQuestionFragment == null) {
                    mQuestionFragment = new QuestionFragment();
                    fragmentTransaction.add(R.id.fl, mQuestionFragment);
                } else {
                    fragmentTransaction.show(mQuestionFragment);
                }
                break;
            case 4:
                if (mMyFragment == null) {
                    mMyFragment = new MyFragment();
                    fragmentTransaction.add(R.id.fl, mMyFragment);
                } else {
                    fragmentTransaction.show(mMyFragment);
                }
                break;
        }
        fragmentTransaction.commit();
    }


    private void hideFragment(FragmentTransaction fragmentTransaction) {
        mIv.setImageResource(R.mipmap.ic_file_1);
        if (mHomeFragment != null)
            fragmentTransaction.hide(mHomeFragment);
        if (mServicePackageFragment != null)
            fragmentTransaction.hide(mServicePackageFragment);
        if (mFileFragment != null)
            fragmentTransaction.hide(mFileFragment);
        if (mQuestionFragment != null)
            fragmentTransaction.hide(mQuestionFragment);
        if (mMyFragment != null)
            fragmentTransaction.hide(mMyFragment);



    }


    //退出
    private void showBackDialog(){
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(this);
        normalDialog.setTitle("退出");
        normalDialog.setMessage("是否退出");
        normalDialog.setCancelable(false);
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        AppManager.getInstance().removeAllActivity();

                    }
                });
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        normalDialog.show();

    }

}
